<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>首页</title>
    <link rel="stylesheet" type="text/css" href="./aui/css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="./aui/css/aui-iconfont.css"/>
    <link rel="stylesheet" type="text/css" href="./css/css.css"/>
    <link rel="stylesheet" type="text/css" href="./css/idangerous.swiper.css"/>
</head>
<style>
    #wraps {
        height: 100%;
    }

    #footer ul li p {
        font-size: 12px;
    }


    #footer ul li i {
        margin-top: 0;
        color: #cccccc;
    }

    #footer ul li.router-link-active i {
        margin-top: 0;
        color: #e8d9f9;
    }

    [v-cloak] {
        display: none;
    }

    @-webkit-keyframes flash {
        0% {
            opacity: 0.5;
        }
        50% {
            opacity: 1;
            color: #ff0000;
        }
        100% {
            opacity: 0.5;
        }
    }

    @keyframes flash {
        0% {
            opacity: 0.5;
        }
        50% {
            opacity: 1;
            color: #ff0000;
        }
        100% {
            opacity: 0.5;
        }
    }

    .flash {
        color: #0000ff;
        -webkit-animation: flash 1.5s infinite cubic-bezier(1, 0, 0.5, 0);
        animation: flash 1.5s infinite cubic-bezier(1, 0, 0.5, 0);
    }

    .flex-con em {
        display: none;
        font-size: 10px;
        position: absolute;
        background: red;
        top: 0px;
        color: #fff;
        padding: 0px 2px;
        min-width: 16px;
        border-radius: 50%;
        left: 50%;
        margin-left: 5px;
    }

    .flex-con em.active {
        display: block;
    }
</style>

<body>
<div id="wraps" class="flex-wrap bgn flex-vertical">
    <div id="main" class="flex-con">
    </div>
    <div id="footer" class="border-t">
        <ul class="flex-wrap">

            <li class="flex-con" :class="{'router-link-active':index==0}" @click="randomSwitchBtn(0)">
                <i class="icon iconfont icon-fujin"></i>
                <p>附近人</p>
            </li>
            <li class="flex-con" :class="{'router-link-active':index==1}" @click="randomSwitchBtn(1)">
                <i class="icon iconfont icon-xiaoxi" :class="{flash:isyuehui}"><em
                        v-if="totalCount>0" :class="{active:totalCount>0}" v-cloak>{{totalCount}}</em></i>
                <p>消息</p>
            </li>
            <li class="flex-con" :class="{'router-link-active':index==2}" @click="randomSwitchBtn(2)">
                <i class="icon iconfont icon-wode"></i>
                <p>我的</p>
            </li>
        </ul>
    </div>
</div>
</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./aui/js/aui.js"></script>
<script src="./script/vue2.js"></script>
<script src="./script/vue-resource.min.js"></script>
<script type="text/javascript" src="./script/common.js"></script>
<script type="text/javascript" src="./script/sha1.min.js"></script>
<script type="text/javascript">
    apiready = function () {
        var vm = new Vue({
            el: "#wraps",
            data: {
                title: '忘记密码',
                newpassword: '',
                mobile: '',
                captcha: '',
                getcode_title: '获取验证码',
                countdown: 0,
                nickname: '',
                avatar: '',
                release: false,
                bannerList: [],
                recommandList: [],
                categoryList: [],
                index: 0,
                isyuehui: false,
                isstage: false,
                totalCount: 0,
            },
            mounted: function () {
                var that = this;
                // $api.rmStorage('userInfo')
                if (!$api.getStorage('userInfo')) { //判断是否登录
                    if (api.systemType == "ios") {
                        var times = 0;
                    } else {
                        var times = 300;
                    }
                    api.openWin({ //未登录打开登陆窗口
                        name: 'login',
                        url: './html/login/login.html',
                        delay: times,
                        slidBackType: 'edge',
                        vScrollBarEnabled: false,
                        slidBackEnabled: false,
                    })
                } else {
                    // var mobile = $api.getStorage('userInfo').mobile
                    // ajpushs(mobile)
                }
                api.addEventListener({
                    name: 'count'
                }, function (ret, err) {
                    // alert(ret.value.count)
                    that.totalCount = ret.value.count
                });
                api.addEventListener({ //监听安卓返回键
                    name: 'keyback'
                }, function (ret, err) {
                    if (that.index == 0) {
                        api.closeWidget() //关闭应用
                    } else {
                        that.randomSwitchBtn(0)
                    }
                });
                setInterval(function () {
                    that.getvoices()
                }, 2000);
                that.getrefUserinfo()
                that.getcomment()
                api.addEventListener({
                    name: 'getyuehui'
                }, function (ret, err) {
                    console.log("约会定位");
                    that.getlocal()
                });
                api.addEventListener({
                    name: 'getpingjia'
                }, function (ret, err) {
                    that.getcomment()
                });
            },
            methods: {
                // 获取评价信息
                getcomment: function () {
                    var that = this;
                    showProgress();
                    console.log("有评价信息哦222");
                    var token = $api.getStorage('userInfo').token
                    console.log(token);
                    api.ajax({
                        url: ajaxRoute.comment,
                        method: 'post',
                        data: {
                            values: {
                                token: token,
                            }
                        }
                    }, function (res, err) {
                        console.log("有评价信息哦");
                        console.log(JSON.stringify(res));
                        if (res.data) {
                            console.log(res.data.score)
                            console.log(res.data.uid);
                            api.sendEvent({
                                name: 'pingjia',
                                extra: {
                                    id: res.data.id,
                                    nickname: res.data.nickname,
                                    uid: res.data.id,
                                    orderno: res.data.orderno,
                                    avatar: res.data.avatar,
                                    birthday: res.data.birthday,
                                    gender: res.data.gender,
                                    age: res.data.age,
                                    score: res.data.score,
                                }
                            });
                        } else {

                        }
                    });
                },
                // 获取位置信息
                getlocal: function () {
                    var that = this;
                    if (setin) {
                        clearInterval(setin);
                    }
                    var bMap = api.require('bMap');
                    if (api.systemType == "ios") {
                        api.startLocation({
                            accuracy: '100m',
                            filter: 1,
                            autoStop: true
                        }, function (ret, err) {
                            if (ret && ret.status) {
                                //获取位置信息成功
                                bMap.getLocationServices(function (ret, err) {
                                    if (ret.authorizationStatus == 'denied') {
                                        // api.alert({
                                        //     title: '未开启定位功能!',
                                        //     msg: '请在手机设置中打开定位功能',
                                        // });
                                    } else {
                                        bMap.initMapSDK(function (ret) { //ios初始化
                                            if (ret.status) {
                                                bMap.getLocation({ //定位
                                                    accuracy: '10m',
                                                    autoStop: true,
                                                    filter: 100
                                                }, function (ret, err) {
                                                    if (ret.status) {
                                                        that.lat = ret.lat;
                                                        that.lng = ret.lon;
                                                        that.sublocal()
                                                        that.getlover()
                                                    } else {
                                                        setTimeout(getlocal(), 10000)
                                                    }
                                                });
                                            }
                                        });
                                    }
                                });
                            } else {
                                // alert(JSON.stringify(err));
                            }
                        });
                    } else {
                        bMap.getLocationServices(function (ret, err) {
                            if (!ret.enable) {
                                // api.alert({
                                //     title: '未开启定位功能!',
                                //     msg: '请在手机设置中打开定位功能',
                                // });
                            } else {
                                bMap.getLocation({ //定位
                                    accuracy: '10m',
                                    autoStop: true,
                                    filter: 100
                                }, function (ret, err) {
                                    if (ret.status) {
                                        that.lat = ret.lat;
                                        that.lng = ret.lon;
                                        that.sublocal()
                                    } else {
                                        setTimeout(getlocal(), 10000)
                                    }
                                });
                            }
                        });
                    }
                    var setin = setInterval(function () {
                        that.getlocal()
                    }, 300000);
                },
                // 声音
                getvoices: function () {
                    var that = this;
                    if (this.isyuehui) {
                        // api.notification({
                        //     sound: 'default'
                        // });
                    } else {

                    }
                },
                // 获取推送
                getlover: function () {
                    // 暂时关闭推送
                    return
                    var that = this;
                    showProgress();

                    var token = $api.getStorage('userInfo').token
                    console.log(token);
                    api.ajax({
                        url: ajaxRoute.lover,
                        method: 'post',
                        data: {
                            values: {
                                token: token,
                                lng: that.lng,
                                lat: that.lat
                            }
                        }
                    }, function (res, err) {
                        that.isyuehui = true
                        if (res.data) {
                            console.log("有约会信息哦");
                            console.log(JSON.stringify(res));
                            // that.getvoices()
                            api.sendEvent({
                                name: 'yuehui',
                                extra: {
                                    id: res.data.id,
                                    uid: res.data.uid,
                                    orderno: res.data.orderno,
                                    username: res.data.love.nickname,
                                    avatar: res.data.love.avatar,
                                    birthday: res.data.love.birthday,
                                    age: res.data.love.age,
                                    score: res.data.score,
                                    distance: res.data.distance,
                                    url: res.data.love.url
                                }
                            });
                        } else {

                            // api.toast({msg: res.msg, location: 'middle'});
                        }
                    });
                },
                // 切换菜单
                randomSwitchBtn: function (index) {

                    console.log(index)
                    api.sendEvent({
                        name: 'charge'
                    });

                    var that = this;
                    if (index == 1) {
                        var reload = true
                    } else {
                        var reload = false
                    }
                    if (that.index == index) {
                        return
                    } else {
                        that.index = index
                        api.setFrameGroupIndex({
                            name: 'group',
                            index: index,
                            reload: reload
                        });
                        api.sendEvent({
                            name: 'frameGroup' + index
                        });

                        if (index == 1) {
                            api.setFrameAttr({
                                name: 'chat',
                                hidden: false
                            });

                        } else if (index == 0) {
                            api.sendEvent({
                                name: 'hhxx'
                            });
                            api.sendEvent({
                                name: 'shuaxin'
                            });

                            api.sendEvent({
                                name: 'yuehuisx',
                            });
                            api.setFrameAttr({
                                name: 'chat',
                                hidden: true
                            });
                        } else {
                            api.setFrameAttr({
                                name: 'chat',
                                hidden: true
                            });
                        }
                    }

                },
                sublocal: function () {
                    var that = this;
                    showProgress();
                    var token = $api.getStorage('userInfo').token
                    console.log(token);
                    api.ajax({
                        url: ajaxRoute.location,
                        method: 'post',
                        data: {
                            values: {
                                token: token,
                                lng: that.lng,
                                lat: that.lat
                            }
                        }

                    }, function (res, err) {
                        console.log(JSON.stringify(res));
                        if (res.code == 1) {
                            // console.log("定位获取");
                            that.getlover()
                        } else {
                            if (that.isstage) {
                                api.alert({
                                    title: '未开启约会开关',
                                    msg: '开启约会开关后才能获取约会推送哦',
                                });
                            }

                            // api.toast({msg: res.msg, location: 'middle'});
                        }
                    });
                },
                getUserInfo: function () {
                    var that = this;
                    that.nickname = $api.getStorage('userInfo').nickname
                    that.avatar = ajaxUrl + '/' + $api.getStorage('userInfo').avatar
                },
                // 进入购物车
                goCart: function () {
                    api.openWin({
                        name: 'shoppingcart',
                        url: './html/my/shoppingcart.html',
                        delay: 0,
                        slidBackType: 'edge',
                        vScrollBarEnabled: false,
                        slidBackEnabled: false,
                    })
                },
                getrefUserinfo: function () {
                    var that = this;
                    showProgress();

                    var token = $api.getStorage('userInfo').token
                    console.log(token);
                    api.ajax({
                        url: ajaxRoute.center,
                        method: 'post',
                        data: {
                            values: {
                                token: token
                            }
                        }
                    }, function (res, err) {
                        if (res.code == 1) {
                            api.hideProgress();
                            console.log(JSON.stringify(res));
                            that.id = res.data.userinfo.id
                            that.nickname = res.data.userinfo.nickname
                            that.avatar = ajaxUrl + '/' + res.data.userinfo.avatar
                            that.mobile = res.data.userinfo.mobile
                            console.log("融云初始化");
                            console.log(that.avatar);
                            $api.setStorage('userInfo', res.data.userinfo)
                            rongyuntoken(that.id, that.nickname, that.avatar)
                            // that.getUserInfo()
                            console.log(res.data.stage);
                            if (res.data.stage == 3) {
                                api.setFrameAttr({
                                    name: 'chat',
                                    hidden: false
                                });
                                that.isstage = false
                            } else {

                                that.isstage = true
                                api.setFrameAttr({
                                    name: 'chat',
                                    hidden: true
                                });
                                api.openFrame({
                                    name: 'regs',
                                    url: 'widget://html/reg/perfect.html',
                                    rect: {
                                        x: 0,
                                        y: 0,
                                        w: 'auto',
                                        h: 'auto'
                                    },
                                    pageParam: {
                                        id: res.data.stage
                                    }
                                });
                            }
                            ajpushs(that.mobile)
                        } else {
                            api.hideProgress();
                            api.toast({msg: res.msg, location: 'middle'});
                        }
                    });
                },
                fabu: function () {
                    var that = this;
                    that.release = !that.release
                },
                //下拉刷新
                onRefresh: function () {
                    setTimeout(function () {
                        api.refreshHeaderLoadDone()
                        window.location.reload();
                    }, 1000);
                },
            },
            filters: {
                filtersImg: function (img) {
                    var imgs = ajaxUrl + '/' + img
                    return imgs;
                },
                filtersNav: function (e) {
                    var s = parseInt(e) + 1;
                    var img = 'image/nav-' + s + '.png'
                    return img;
                }
            }
        })
        var loading = '';

        function funIniGroup(index) {
            var main;
            frames = []
            for (var i = 0; i < index; i++) {
                frames.push({
                    name: 'frame' + i,
                    url: './html/frame' + i + '.html',
                    bgColor: '#fff',
                    bounces: false,
                    pageParam: {
                        'tabindex': 0,
                    },
                })
            }
            api.openFrameGroup({
                name: 'group',
                scrollEnabled: false,
                rect: {
                    x: 0,
                    y: 0,
                    w: api.winWidth,
                    h: $api.dom('#main').offsetHeight
                },
                index: 0,
                frames: frames,
                preload: 0
            }, function (ret, err) {
                if (loading.loading_show) {
                    api.setFrameGroupAttr({
                        name: 'group',
                        hidden: true
                    });
                }
            });
        }

        function loginInit() {
            api.getPrefs({
                key: 'isLogin'
            }, function (ret, err) {
                funIniGroup(3);
                if (ret && ret.value == 'true') {
                    //已登录
                } else {
                    //未登录
                    api.openWin({
                        name: 'login',
                        url: 'widget://html/login/login.html',
                        slidBackEnabled: false,
                        pageParam: {
                            name: 'test'
                        }
                    });

                }
            });
        }

        function ajpushs(alias) {
            var ajpush = api.require('ajpush');
            // alert(ajpush)
            console.log('=====极光推送');
            console.log(alias);
            var param = {
                alias: alias,
                tags: ['tag1', 'tag2']
            };
            ajpush.bindAliasAndTags(param, function (ret) {
                var statusCode = ret.statusCode;
            });
            ajpush.init(function (ret) {
                if (ret && ret.status) {
                    ajpush.setListener(function (ret) {
                        var id = ret.id;
                        console.log("id=" + id + ",title=" + title + ",content=" + content + ",extra=" + extra);
                    });
                    api.addEventListener({
                        name: 'appintent'
                    }, function (ret, err) {
                        if (ret && ret.appParam.ajpush) {
                            var ajpush = ret.appParam.ajpush;
                            var id2 = ajpush.extra.id;
                            // alert(JSON.stringify(ret))
                            if (id2) {
                                api.openWin({
                                    name: 'message',
                                    url: 'widget://html/message/details.html',
                                    slidBackType: 'edge',
                                    vScrollBarEnabled: false,
                                    slidBackEnabled: false,
                                    pageParam: {
                                        name: id2
                                    }
                                })
                            }
                        }
                    })
                    api.addEventListener({
                        name: 'noticeclicked'
                    }, function (ret, err) {
                        if (ret && ret.value) {
                            var ajpush = ret.value;
                            var id2 = ajpush.id;
                            if (id2) {
                                api.openWin({
                                    name: 'message',
                                    url: 'widget://html/message/details.html',
                                    slidBackType: 'edge',
                                    vScrollBarEnabled: false,
                                    slidBackEnabled: false,
                                    pageParam: {
                                        id: id2
                                    }
                                })
                            }
                        }
                    })
                }
            });
        }

        // 融云初始化
        function rongyuntoken(id, zh, tx) {
            //获取token
            var userId = id;
            //会员id
            var name = zh;
            //会员昵称
            var portraitUri = tx;
            //会员头像
            var appKey = "pvxdm17jpofbr";
            var appSecret = "QEz3jusf7VW";
            var nonce = Math.floor(Math.random() * 1000000);
            //随机数
            var timestamp = Date.now();
            //时间戳
            var signature = sha1("" + appSecret + nonce + timestamp);
            //数据签名(通过哈希加密计算)
            api.ajax({
                url: "http://api.cn.ronghub.com/user/getToken.json",
                method: "post",
                headers: {
                    "RC-App-Key": appKey,
                    "RC-Nonce": "" + nonce,
                    "RC-Timestamp": "" + timestamp,
                    "RC-Signature": "" + signature,
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                data: {
                    values: {
                        userId: userId,
                        name: name,
                        portraitUri: portraitUri
                    }
                }
            }, function (ret, err) {
                console.log(JSON.stringify(ret))
                console.log("融云初始化");
                if (ret) {
                    $api.setStorage('token', ret.token);
                    $api.setStorage('zh', zh);
                    $api.setStorage('id', id);
                    $api.setStorage('tx', tx);
                    //融云初始化
                    var rong = api.require('rongCloud2');
                    rong.init(function (ret, err) {
                        if (ret.status == 'success') {
                            //消息监听
                            jt();

                            rong.connect({
                                token: $api.getStorage('token')
                            }, function (ret, err) {
                            });
                        } else {
                            api.toast({
                                msg: '服务器连接失败'
                            });
                        }
                    });

                    // setTimeout(function () {
                    //     api.toast({
                    //         msg: '登录超时，请检查网络',
                    //         duration: 3000,
                    //         location: 'bottom'
                    //     });
                    // }, 5000)
                } else {
                    api.toast({
                        msg: '获取token失败',
                        duration: 3000,
                        location: 'bottom'
                    });
                }
            });
        }

        //融云消息监听器
        function jt() {
            var rong = api.require('rongCloud2');
            rong.setOnReceiveMessageListener(function (ret, err) {
                if (ret) {
                    api.sendEvent({
                        name: 'hhxx'
                    });
                    api.sendEvent({
                        name: 'new_msg',
                        extra: {
                            msg: ret.result.message
                        }
                    })

                }
            })
        }

        // 更新

        // 监听热更新
        api.addEventListener({
            name: 'smartupdatefinish'
        }, function (ret, err) {
            api.rebootApp();
        });
        // 状态栏颜色
        api.setStatusBarStyle({
            style: 'light',
            color: '#000'
        });
        // 监听网络
        api.addEventListener({
            name: 'online'
        }, function (ret, err) {
            connectionType = ret.connectionType;
        });
        api.addEventListener({
            name: 'offline'
        }, function (ret, err) {
            connectionType = "";
            toast('网络无连接！');
        });
        funIniGroup(3)
        // 监听底部切换
        api.addEventListener({
            name: 'footerGroup'
        }, function (ret, err) {
            api.openWin({
                name: 'frame1',
                url: 'html/frame1.html',
                pageParam: {
                    frame1: 1
                }
            });
        });
        api.addEventListener({
            name: 'loginSuccess'
        }, function (ret, err) {
            funIniGroup(3)
        });
        api.addEventListener({
            name: 'isyuehui'
        }, function (ret, err) {
            vm.isyuehui = false
        });
        api.addEventListener({
            name: 'isLogin'
        }, function (ret, err) {
            vm.getrefUserinfo()
        });

        function getlocal() {
            vm.getlocal()
        }


    }
</script>

</html>
